package com.nwpu.coursesupport.tool;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nwpu.coursesupport.enumeration.StatusType;
import com.nwpu.coursesupport.view.StudentGradeView;

public class ExcelBuilderUtil {

	private static Logger logger = LoggerFactory.getLogger(ExcelBuilderUtil.class);
	
	
	public static void createGradeExcel(String fileName, String [] [] content,String [] title,HttpServletResponse response){
		HSSFWorkbook book=getHSSFWorkbook(fileName,content,title);
		if(book==null){
			WebUtils.sendJson(response, StatusType.WRONG,"生成Excel失败");
		}else{
			writerExcel(book,response,fileName);
		}
	}
	
	public static HSSFWorkbook getHSSFWorkbook(String sheetName, String [][] content,String [] title) {
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet(sheetName);
		// 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
		HSSFRow row = sheet.createRow(0);
		// 第四步，创建单元格，并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

		HSSFCell cell = null;
		// 创建标题
		//int length=title.length>values.length?values.length:values.length;
		
		for (int i = 0; i < title.length; i++) {
			cell = row.createCell(i);
			cell.setCellValue(title[i]);
			cell.setCellStyle(style);
		}
		// 创建内容
		//"班级","学号","姓名","任务成绩","平时成绩","理论成绩","实验成绩","总成绩"
		for(int i=0;i<content.length;i++){
			row = sheet.createRow(i + 1);
			int length=title.length<content[i].length?title.length:content[i].length;
			for(int j=0;j<length;j++){
				cell=row.createCell(j);
				cell.setCellStyle(style);
				cell.setCellValue(content[i][j]);
			}
		}
		
		/*for (int i = 0; i < list.size(); i++) {
			row = sheet.createRow(i + 1);
			StudentGradeView item=list.get(i);
			int index=0;
			createCell(row,style,item.getClassNumber(),index++);
			createCell(row,style,item.getStudentCode(),index++);
			createCell(row,style,item.getName(),index++);
			createCell(row,style,String.valueOf(item.getTaskTotalGrade()),index++);
			createCell(row,style,String.valueOf(item.getDailyGrade()),index++);
			createCell(row,style,String.valueOf(item.getTheoryTestGrade()),index++);
			createCell(row,style,String.valueOf(item.getLabTestGrade()),index++);
			createCell(row,style,String.valueOf(item.getFinalTotalGrade()),index++);

		}*/

		return wb;
	}
	
	/*private static void createCell(HSSFRow row,HSSFCellStyle style,String value,int index){
		HSSFCell cell=row.createCell(index);
		cell.setCellStyle(style);
		cell.setCellValue(value);
	}*/
	
	public static void writerExcel(HSSFWorkbook wb,HttpServletResponse response,String fileName){
		OutputStream out=null;
		try {
			fileName = URLEncoder.encode(fileName, "UTF-8");
			response.reset();
			response.setContentType("application/force-download");// 设置强制下载不打开
			response.addHeader("Content-Disposition", "attachment;fileName=" + fileName+".xls");// 设置文件名
			out=response.getOutputStream();
			wb.write(out);
			out.flush();
		} catch (UnsupportedEncodingException e) {
			logger.error(e.getMessage());
		} catch (IOException e) {
			logger.error(e.getMessage());
		}finally{
			if(out!=null){
				try {
					out.close();
				} catch (IOException e) {
					logger.error(e.getMessage());
				}
			}
		}
	}
}
